import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.File;
import java.io.IOException;

/**
 * Created by hadoop on 2016/2/16.
 */
public class DomReadXml {
    public static void main(String[] args) throws ParserConfigurationException, IOException, SAXException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document document = builder.parse(new File(DomReadXml.class.getClass().getResource("/").getPath(),"test.xml"));
        Element root = document.getDocumentElement();
        System.out.println("cat="+root.getAttribute("cat"));

        NodeList lans = root.getElementsByTagName("lan");

        for (int i = 0; i < lans.getLength(); i++) {
            Element item = (Element) lans.item(i);
            System.out.println(item.getTagName()+" id="+item.getAttribute("id"));

            NodeList child = item.getChildNodes();
            for (int j = 0; j < child.getLength(); j++) {
                Node cnode = child.item(j);
                if(cnode instanceof Element)
                    System.out.println(cnode.getNodeName()+"="+cnode.getTextContent());
            }
        }
    }
}
